<template>
  <div class="article-detail">
    <!-- 导航栏 -->
    <el-header class="header">
      <div class="container flex-between">
        <div class="logo">
          <h2>博客系统</h2>
        </div>
        <div class="nav-menu">
          <div class="nav-content">
            <!-- 导航链接 -->
            <div class="nav-links">
              <a
                href="#"
                @click.prevent="handleMenuSelect('home')"
                class="nav-link"
              >
                首页
              </a>
              <a
                v-if="!authStore.isLoggedIn"
                href="#"
                @click.prevent="handleMenuSelect('login')"
                class="nav-link"
              >
                登录
              </a>
            </div>

            <!-- 用户信息区域 -->
            <div v-if="authStore.isLoggedIn" class="user-info-section">
              <div class="user-profile">
                <el-avatar :size="32" :src="authStore.user?.avatar">
                  {{ authStore.user?.nickname || authStore.user?.username }}
                </el-avatar>
                <span class="username">{{ authStore.user?.nickname || authStore.user?.username }}</span>
              </div>
              <div class="user-actions">
                <el-button size="small" @click="handleMenuSelect('admin')">
                  管理后台
                </el-button>
                <el-button size="small" type="danger" @click="handleMenuSelect('logout')">
                  退出登录
                </el-button>
              </div>
            </div>
          </div>
        </div>
      </div>
    </el-header>

    <!-- 主要内容 -->
    <el-main class="main-content" v-loading="articleStore.loading">
      <div class="container">
        <div class="article-container" v-if="articleStore.currentArticle">
          <!-- 文章头部 -->
          <div class="article-header">
            <h1 class="article-title">{{ articleStore.currentArticle.title }}</h1>
            <div class="article-meta">
              <div class="meta-left">
                <span class="author">
                  <el-icon><User /></el-icon>
                  {{ articleStore.currentArticle.author?.nickname || articleStore.currentArticle.author?.username }}
                </span>
                <span class="date">
                  <el-icon><Calendar /></el-icon>
                  {{ formatDate(articleStore.currentArticle.created_at) }}
                </span>
              </div>
              <div class="meta-right">
                <span class="views">
                  <el-icon><View /></el-icon>
                  {{ articleStore.currentArticle.view_count }}
                </span>
                <span class="likes">
                  <el-icon><Star /></el-icon>
                  {{ articleStore.currentArticle.like_count }}
                </span>
              </div>
            </div>
            <div class="article-tags" v-if="articleStore.currentArticle.tags">
              <el-tag
                v-for="tag in articleStore.currentArticle.tags.split(',')"
                :key="tag"
                class="tag-item"
              >
                {{ tag.trim() }}
              </el-tag>
            </div>
          </div>

          <!-- 封面图片 -->
          <div class="article-cover" v-if="articleStore.currentArticle.cover_image">
            <img :src="articleStore.currentArticle.cover_image" :alt="articleStore.currentArticle.title" />
          </div>

          <!-- 文章内容 -->
          <div class="article-content">
            <div class="content-body" v-html="formatContent(articleStore.currentArticle.content)"></div>
          </div>

          <!-- 文章操作 -->
          <div class="article-actions">
            <el-button @click="$router.go(-1)">
              <el-icon><ArrowLeft /></el-icon>
              返回
            </el-button>
            <div class="action-buttons">
              <el-button type="primary" @click="handleLike">
                <el-icon><Star /></el-icon>
                点赞 ({{ articleStore.currentArticle.like_count }})
              </el-button>
              <el-button @click="handleShare">
                <el-icon><Share /></el-icon>
                分享
              </el-button>
            </div>
          </div>
        </div>

        <!-- 空状态 -->
        <div class="empty-state" v-else-if="!articleStore.loading">
          <el-empty description="文章不存在或已被删除" />
          <el-button type="primary" @click="$router.push('/')">
            返回首页
          </el-button>
        </div>
      </div>
    </el-main>
  </div>
</template>

<script setup lang="ts">
import { onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useAuthStore } from '@/stores/auth'
import { useArticleStore } from '@/stores/article'
import {
  User,
  Calendar,
  View,
  Star,
  ArrowLeft,
  Share
} from '@element-plus/icons-vue'

const route = useRoute()
const router = useRouter()
const authStore = useAuthStore()
const articleStore = useArticleStore()

// 处理菜单选择
const handleMenuSelect = (index: string) => {
  switch (index) {
    case 'home':
      router.push('/')
      break
    case 'login':
      router.push('/login')
      break
    case 'admin':
      router.push('/admin')
      break
    case 'logout':
      authStore.logout()
      break
  }
}

// 格式化日期
const formatDate = (dateString: string) => {
  return new Date(dateString).toLocaleString('zh-CN')
}

// 格式化内容（简单的换行处理）
const formatContent = (content: string) => {
  return content.replace(/\n/g, '<br>')
}

// 点赞功能（暂时只是提示）
const handleLike = () => {
  ElMessage.success('点赞功能开发中...')
}

// 分享功能
const handleShare = () => {
  if (navigator.share) {
    navigator.share({
      title: articleStore.currentArticle?.title,
      url: window.location.href
    })
  } else {
    // 复制链接到剪贴板
    navigator.clipboard.writeText(window.location.href)
    ElMessage.success('链接已复制到剪贴板')
  }
}

// 获取文章详情
onMounted(async () => {
  const articleId = Number(route.params.id)
  if (articleId) {
    try {
      await articleStore.fetchArticle(articleId)
    } catch (error) {
      console.error('获取文章详情失败:', error)
    }
  }
})
</script>

<style lang="scss" scoped>
.article-detail {
  min-height: 100vh;
  background-color: #f5f5f5;
}

.header {
  background: white;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  height: 64px;

  .container {
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
  }

  .logo h2 {
    color: var(--primary-color);
    margin: 0;
    line-height: 1;
  }

  .nav-menu {
    height: 100%;

    .nav-content {
      display: flex;
      align-items: center;
      justify-content: space-between;
      width: 100%;
      height: 100%;

      .nav-links {
        display: flex;
        align-items: center;
        gap: 30px;
        height: 100%;

        .nav-link {
          color: var(--text-primary);
          text-decoration: none;
          font-size: 16px;
          font-weight: 500;
          padding: 0 16px;
          border-radius: 6px;
          transition: all 0.3s ease;
          position: relative;
          display: flex;
          align-items: center;
          height: 100%;

          &:hover {
            color: var(--primary-color);
            background-color: rgba(102, 126, 234, 0.1);
          }

          &.active {
            color: var(--primary-color);

            &::after {
              content: '';
              position: absolute;
              bottom: 0;
              left: 50%;
              transform: translateX(-50%);
              width: 20px;
              height: 2px;
              background-color: var(--primary-color);
              border-radius: 1px;
            }
          }
        }
      }

      .user-info-section {
        display: flex;
        align-items: center;
        gap: 15px;
        height: 100%;

        .user-profile {
          display: flex;
          align-items: center;
          gap: 8px;

          .username {
            font-weight: 500;
            color: var(--text-primary);
            font-size: 14px;
            line-height: 1;
          }
        }

        .user-actions {
          display: flex;
          align-items: center;
          gap: 8px;

          .el-button {
            padding: 6px 12px;
            font-size: 12px;
            height: auto;
          }
        }
      }
    }
  }
}

.main-content {
  padding: 40px 0;
  
  .article-container {
    max-width: 800px;
    margin: 0 auto;
    background: white;
    border-radius: 10px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    overflow: hidden;
    
    .article-header {
      padding: 40px 40px 20px 40px;
      
      .article-title {
        margin: 0 0 20px 0;
        color: var(--text-primary);
        font-size: 2rem;
        line-height: 1.4;
      }
      
      .article-meta {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 20px;
        font-size: 0.9rem;
        color: var(--text-secondary);
        
        .meta-left,
        .meta-right {
          display: flex;
          gap: 20px;
          
          span {
            display: flex;
            align-items: center;
            gap: 4px;
          }
        }
      }
      
      .article-tags {
        .tag-item {
          margin-right: 8px;
        }
      }
    }
    
    .article-cover {
      width: 100%;
      max-height: 400px;
      overflow: hidden;
      
      img {
        width: 100%;
        height: 100%;
        object-fit: cover;
      }
    }
    
    .article-content {
      padding: 40px;
      
      .content-body {
        line-height: 1.8;
        color: var(--text-regular);
        font-size: 16px;
        
        :deep(h1), :deep(h2), :deep(h3), :deep(h4), :deep(h5), :deep(h6) {
          margin: 30px 0 15px 0;
          color: var(--text-primary);
        }
        
        :deep(p) {
          margin-bottom: 15px;
        }
        
        :deep(img) {
          max-width: 100%;
          height: auto;
          border-radius: 4px;
          margin: 15px 0;
        }
      }
    }
    
    .article-actions {
      padding: 20px 40px 40px 40px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      border-top: 1px solid var(--border-light);
      
      .action-buttons {
        display: flex;
        gap: 10px;
      }
    }
  }
  
  .empty-state {
    text-align: center;
    padding: 60px 0;
    background: white;
    border-radius: 10px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  }
}

@media (max-width: 768px) {
  .header {
    .nav-menu {
      .nav-content {
        flex-direction: column;
        gap: 15px;
        align-items: stretch;

        .nav-links {
          justify-content: center;
          gap: 20px;

          .nav-link {
            font-size: 14px;
            padding: 6px 12px;
          }
        }

        .user-info-section {
          justify-content: space-between;
          padding: 10px 0;
          border-top: 1px solid #f0f0f0;

          .user-actions {
            .el-button {
              padding: 4px 8px;
              font-size: 11px;
            }
          }
        }
      }
    }
  }

  .article-container {
    margin: 0 10px;

    .article-header,
    .article-content,
    .article-actions {
      padding-left: 20px;
      padding-right: 20px;
    }

    .article-header .article-title {
      font-size: 1.5rem;
    }
    
    .article-meta {
      flex-direction: column;
      align-items: flex-start;
      gap: 10px;
    }
    
    .article-actions {
      flex-direction: column;
      gap: 15px;
      
      .action-buttons {
        width: 100%;
        justify-content: center;
      }
    }
  }
}
</style>
